<template>
  <div class="ADslideshow">
    <el-card class="card">
      <!-- 面包屑 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 轮播图 -->
      <div class="slidePic">
        <el-carousel trigger="click" height="350px">
          <el-carousel-item v-for="item of slideList" :key="item.id">
            <img :src="baseurl+item.picpath" @click="deletepic(item.id)">
          </el-carousel-item>
        </el-carousel>
      </div>
      <el-upload
        class="upload-demo"
        ref="upload"
        :limit="1"
        accept=".jpg,.png"
        action="/api/slideshow/upload"
        :headers="headers"
        :before-upload="beforeup"
        :on-success="backinfo"
        :on-error="errorinfo"
        :file-list="fileList"
        :auto-upload="false"
        list-type="picture"
        >
        <el-button slot="trigger" size="small" type="primary">选取图片</el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，一次只能上传1张</div>
      </el-upload>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ADslideshow',
  data () {
    return {
      slideList: [],
      fileList: [],
      baseurl: 'http://123.56.11.253:8181/transaction/'
    }
  },
  components: {},
  computed: {
    headers () {
      return {
        'token': sessionStorage.getItem('adtoken')
      }
    }
  },
  created () {
    this.getSlideShow()
  },
  methods: {
    // 获取轮播图
    async getSlideShow () {
      const res = await this.$http.get('slideshow/list')
      console.log(res)
      const { status, msg, data} = res.data
      if (status === 200) {
        this.slideList = data
        console.log(this.slideList)
      } else {
        console.log(msg)
      }
    },
    // 删除轮播图
    deletepic (id) {
      console.log(id)
      this.$confirm('此操作删除该图片, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const AUTH_TOKEN = sessionStorage.getItem('adtoken')
        this.$http.defaults.headers.common['token'] = AUTH_TOKEN
        const res = await this.$http.delete(`slideshow/delete?id=${id}`)
        console.log(res)
        const {msg, status} = res.data
        if (status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getSlideShow()
        } else {
          this.$message.error(msg)
        }
      }).catch(() => {
      })
    },
    // 上传图片
    backinfo (response, file, fileList) {
      this.fileList = []
      console.log('上传成功', response)
      if (response.status === 200) {
        const AUTH_TOKEN = sessionStorage.getItem('adtoken')
        this.$http.defaults.headers.common['token'] = AUTH_TOKEN
        const url = response.data
        const from = {
          ahref: '',
          index: 1,
          picpath: url
        }
        this.$http.post('slideshow/add',from).then((res)=>{
          if( res.data.status === 200) {
            this.$message.success('上传成功！')
            this.getSlideShow()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      } else {
        this.$message.error(response.msg)
      }
    },
    // 上传失败回调事件
    errorinfo (response, file, fileList) {
      this.$message.error('上传失败，请重试！')
    },
    // 上传之前的验证
    beforeup (file) {
      console.log(file)
      this.file = file.name
      console.log('this.headers', this.headers)
      const extension = file.name.split('.')[1] === 'jpg'
      const extension2 = file.name.split('.')[1] === 'png'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!extension && !extension2) {
        this.$message.error('上传模板只能是 jpg、png格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传模板大小不能超过 10MB!')
      }
    },
    // 上传文件
    submitUpload () {
      let _this = this
      setTimeout(function () {
        _this.$refs.upload.submit()
      }, 200)
    }
  }
}
</script>

<style scoped lang="stylus">
.ADslideshow,.card
  height 100%
  min-width: 1200px
  .slidePic
    display inline-block
    width 600px
    padding 10px 320px 10px 300px
    margin-top 30px
    background-color #f2f2f2
    img 
      height 350px
      cursor pointer
      width 100%
.upload-demo
  padding-left 10px
  padding-top 30px
</style>
